package cn.piesat.client.converter;

import cn.piesat.client.model.dto.base.task.DefaultRequestDto;
import cn.piesat.client.model.dto.buiness.QsClientRunnerDto;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class QsClientRunnerConverter {

    public QsClientRunnerDto requestDto2RunnerDto(DefaultRequestDto requestDto, JSONObject algorithmParameters, String execJsonPath, String resultJsonFile){
        QsClientRunnerDto qsClientRunnerDto = new QsClientRunnerDto();

        qsClientRunnerDto.setTaskId(requestDto.getBasic().getTaskId());
        qsClientRunnerDto.setFileTaskId(requestDto.getBasic().getFileTaskId());
        qsClientRunnerDto.setFileName(requestDto.getBasic().getMainFileName());
        qsClientRunnerDto.setHasFlow(requestDto.getBasic().getHasFlow());
        qsClientRunnerDto.setAlgorithmIdentify(requestDto.getBasic().getAlgorithmIdentify());
        qsClientRunnerDto.setOther(requestDto.getOther());

        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(requestDto.getOther()));
        qsClientRunnerDto.setAlgorithmId(jsonObject.getString("algorithmId"));

        qsClientRunnerDto.setAlgorithmExecFile(execJsonPath);
        qsClientRunnerDto.setResultJsonFile(resultJsonFile);

        if(algorithmParameters!=null){
            qsClientRunnerDto.setExecJson(algorithmParameters.toJSONString());
            qsClientRunnerDto.setAlgorithmLogFile(algorithmParameters.getString("resultLogFile"));
        }
        return qsClientRunnerDto;
    }
    
    public QsClientRunnerDto requestDto2RunnerDto(DefaultRequestDto requestDto, String algorithmParameters, String exectxtPath, String resultJsonFile,String algorithmLogFile,String algorithmFlowFile){
        QsClientRunnerDto qsClientRunnerDto = new QsClientRunnerDto();

        qsClientRunnerDto.setTaskId(requestDto.getBasic().getTaskId());
        qsClientRunnerDto.setFileTaskId(requestDto.getBasic().getFileTaskId());
        qsClientRunnerDto.setFileName(requestDto.getBasic().getMainFileName());
        qsClientRunnerDto.setHasFlow(requestDto.getBasic().getHasFlow());
        qsClientRunnerDto.setAlgorithmIdentify(requestDto.getBasic().getAlgorithmIdentify());
        qsClientRunnerDto.setOther(requestDto.getOther());

        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(requestDto.getOther()));
        qsClientRunnerDto.setAlgorithmId(jsonObject.getString("algorithmId"));

        qsClientRunnerDto.setAlgorithmExecFile(exectxtPath);
        qsClientRunnerDto.setResultJsonFile(resultJsonFile);

        if(algorithmParameters!=null){
            qsClientRunnerDto.setExecJson(algorithmParameters);
            qsClientRunnerDto.setAlgorithmLogFile(algorithmLogFile);
            qsClientRunnerDto.setAlgorithmFlowFile(algorithmLogFile);
        }
        return qsClientRunnerDto;
    }
}
